﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;

namespace WeBlog
{
    public enum StatusMessageType
    {
        Success,
        Warning,
        Error
    }

    public enum StatusMessageFormat
    {
        BoldCenter,
        NormalBlock
    }

    public static class TempDataExtensions
    {                
        public static void SetStatusMessage(this TempDataDictionary tempData, string message, StatusMessageType messageType = StatusMessageType.Success, StatusMessageFormat messageFormat = StatusMessageFormat.BoldCenter)
        {
            tempData["StatusMessageText"] = message;
            tempData["StatusMessageFormat"] = messageFormat;
            tempData["StatusMessageType"] = messageType;
        }

        /// <summary>
        /// Emits a stylized status message.
        /// </summary>
        /// <param name="messageType">The type of message being displayed, determines the style to be used.</param>
        /// <param name="messageFormat">The general style and format of the message text.</param>
        /// <param name="messageText">Text or html of the message.</param>
        /// <param name="htmlAttributes">Additional html attributes for the outer message container.</param>
        public static MvcHtmlString StatusMessage(this HtmlHelper htmlHelper, StatusMessageType messageType = StatusMessageType.Success, StatusMessageFormat messageFormat = StatusMessageFormat.BoldCenter, string messageText = null, object htmlAttributes = null)
        {
            // required for legacy webform pages
            if (htmlHelper == null)
                return MvcHtmlString.Empty;

            var tempData = htmlHelper.ViewContext.TempData;

            if (string.IsNullOrEmpty(messageText))
            {
                messageText = (string)tempData["StatusMessageText"];
            }

            if (string.IsNullOrEmpty(messageText))
                return MvcHtmlString.Empty;

            // if dictionary contains keys for format and type use appropriate StatusMessage overload
            if (tempData["StatusMessageFormat"] != null)
            {
                messageFormat = (StatusMessageFormat)tempData["StatusMessageFormat"];
            }
            if (tempData["StatusMessageType"] != null)
            {
                messageType = (StatusMessageType)tempData["StatusMessageType"];
            }

            var innerDiv = new TagBuilder("div") { InnerHtml = messageText };
            innerDiv.AddCssClass("divStatusMessage");

            switch (messageFormat)
            {
                case StatusMessageFormat.BoldCenter:
                    innerDiv.AddCssClass("divStatusFormatBoldCenter");
                    break;
                case StatusMessageFormat.NormalBlock:
                    innerDiv.AddCssClass("divStatusFormatNormalBlock");
                    break;
            }

            var outerDiv = new TagBuilder("div") { InnerHtml = innerDiv.ToString() };

            var attribs = htmlAttributes == null ? new RouteValueDictionary() : new RouteValueDictionary(htmlAttributes);
            outerDiv.MergeAttributes(attribs);
            outerDiv.AddCssClass("divStatusContainer");

            switch (messageType)
            {
                case StatusMessageType.Success:
                    outerDiv.AddCssClass("divStatusSuccess");
                    break;
                case StatusMessageType.Warning:
                    outerDiv.AddCssClass("divStatusWarning");
                    break;
                case StatusMessageType.Error:
                    outerDiv.AddCssClass("divStatusError");
                    break;
            }

            return MvcHtmlString.Create( outerDiv.ToString() );
        }
    }
}
